package com.ruoyi.system.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class GetOrderNoUtil {

    @Autowired
    private RedisUtil redisUtil;

    public String getOrderNo(String type) {
        StringBuilder inboundOrderNo = new StringBuilder();
        inboundOrderNo.append(type);
        inboundOrderNo.append(DateFormatterUtil.getCurrentDateTime("yyyyMMdd"));
        String redisKey = "orderNo";
        if(!redisUtil.hasKey(redisKey)){
            redisUtil.set(redisKey, 1);
        }

        Integer num = (Integer) redisUtil.get(redisKey);

        String orderNo = num < 100 ? String.format("%03d", num) : num.toString();
        inboundOrderNo.append("-"+orderNo);

        redisUtil.incr(redisKey, 1);
        return inboundOrderNo.toString();
    }

    /**
     * 如果前端点击取消后，则redis的orderNo的key要-1
     */
    public void clearOrderNo() {
        redisUtil.decr("orderNo", 1);
    }
}
